/*
 * Copyright 2009 Armando Blancas
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package tests.taskgraph.tasks.data;

import java.io.IOException;

import junit.framework.Assert;
import junit.framework.JUnit4TestAdapter;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import taskgraph.channels.Channel;
import taskgraph.ports.InputPort;
import taskgraph.ports.OutputPort;
import taskgraph.tasks.data.Select;

public class SelectTest {

	/**
	 * Runs the test suite in this class from the command line.
	 * 
	 * @param args	Arguments are ignored.
	 */
	public static void main(String[] args) {
		org.junit.runner.JUnitCore.runClasses(SelectTest.class);
	}
	
	/**
	 * Provides compatibility with 3.x versions of JUnit.
	 * 
	 * @return A 3.x-compatible test suite.
	 */
	public static junit.framework.Test suite() {
		return new JUnit4TestAdapter(SelectTest.class);
	}
	
	private InputPort<Product> input;
	private OutputPort<String> output;
	
	@Before
	public void setup() {
		Channel<Product> channelp = new Channel<Product>();
		Channel<String> channels = new Channel<String>();
		input = channelp.getInputPort();
		output = channels.getOutputPort();
	}
	
	@After
	public void cleanup() throws IOException {
		output.close();
		input.close();
	}
	
    /*+----------------------------------------------------------------------+
	  |                                                                      | 
	  | Test the constructors.                                               | 
	  |                                                                      | 
	  +----------------------------------------------------------------------+*/
	
	@Test
	public void defaultConstructor() {
		Select<Product, String> task = new Select<Product, String>();
		Assert.assertNotNull(task);
	}
	
	@Test
	public void defaultInstanceClass() {
		Select<Product, String> task = new Select<Product, String>();
		Assert.assertEquals(Select.class, task.getClass());
	}

	@Test
	public void constructorWithPorts() {
		Select<Product, String> task = new Select<Product, String>(input, output, "Property");
		Assert.assertNotNull(task);
	}
	
	@Test
	public void constructorWithPortsAndClass() {
		Select<Product, String> task = new Select<Product, String>(input, output, "Property");
		Assert.assertEquals(Select.class, task.getClass());
	}

    /*+----------------------------------------------------------------------+
	  |                                                                      | 
	  | Test the run method.                                                 | 
	  |                                                                      | 
	  +----------------------------------------------------------------------+*/
	
	@Test
	public void testRun() throws IOException {
		Product[] products = {
			new Product(5005, "Notebook", 2.50f),
			new Product(1400, "Pencil",   0.49f),
			new Product(0001, "Eraser",   0.60f),
			new Product(4040, "Bookend", 10.25f)
		};

		Channel<Product> ch1 = new Channel<Product>();
		Channel<String> ch2 = new Channel<String>();
		Select<Product, String> task = 
			new Select<Product, String>(ch1.getInputPort(), ch2.getOutputPort(), "name");
		
		OutputPort<Product> outputPort = ch1.getOutputPort();
		InputPort<String> inputPort = ch2.getInputPort();
		for (Product p : products)
			outputPort.write(p);
		outputPort.close();
		
		task.run();
		
		Assert.assertEquals(products[0].getName(),  inputPort.read());
		Assert.assertEquals(products[1].getName(),  inputPort.read());
		Assert.assertEquals(products[2].getName(),  inputPort.read());
		Assert.assertEquals(products[3].getName(),  inputPort.read());
		inputPort.close();
	}
	
    /*+----------------------------------------------------------------------+
	  |                                                                      | 
	  | Negative tests for constructors.                                     | 
	  |                                                                      | 
	  +----------------------------------------------------------------------+*/

	@Test(expected=IllegalArgumentException.class)
	public void constructorWithNullInputPort() throws IOException {
		new Select<Product, String>(null, output, "Property");
	}

	@Test(expected=IllegalArgumentException.class)
	public void constructorWithNullOutputPort() throws IOException {
		new Select<Product, String>(input, null, "Property");
	}

	@Test(expected=IllegalArgumentException.class)
	public void constructorWithNullProperty() throws IOException {
		new Select<Product, String>(input, output, null);
	}

    /*+----------------------------------------------------------------------+
	  |                                                                      | 
	  | Negative tests.                                                      | 
	  |                                                                      | 
	  +----------------------------------------------------------------------+*/

}
